package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.constant.MessageConstant;
import com.igeek.collateral.entity.Agency;
import com.igeek.collateral.entity.Assess;
import com.igeek.collateral.entity.Member;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IAgencyService;
import com.igeek.collateral.service.IAssessService;
import com.igeek.collateral.vo.PageQueryVO;
import com.igeek.collateral.vo.PieVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/assess")
@Api(tags = "评估报告接口")
@Slf4j
public class AssessController {
    @Autowired
    IAssessService iAssessService;
    @GetMapping("findPage")
    @ApiOperation("查询评估报告")
    public Result findPage(PageQueryVO vo) {
        try {
            Page<Assess> page = iAssessService.findPage(vo);
            if (page != null) {
                return new Result(true, MessageConstant.QUERY_ASSESS_SUCCESS, page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ASSESS_FAIL);

    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询评估报告")
    public Result findById(@PathVariable("id") Long id){
        try {
            Assess assess = iAssessService.getById(id);
            if(assess!=null){
                return new Result(true , MessageConstant.QUERY_ASSESS_SUCCESS, assess);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.QUERY_ASSESS_FAIL);
    }
    @ApiOperation("新增评估报告")
    @PostMapping("add")
    public Result add(@ApiParam("评估报告") @RequestBody Assess assess) {
        try {
            iAssessService.save(assess);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ASSESS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ASSESS_SUCCESS);
    }
    @PutMapping("update")
    @ApiOperation("编辑评估报告")
    public Result update(@RequestBody Assess assess){
        try {
            boolean flag = iAssessService.updateById(assess);
            if(flag){
                return new Result(true , MessageConstant.EDIT_ASSESS_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , MessageConstant.EDIT_ASSESS_FAIL);
    }

//,@RequestParam("AssessId")  Integer AssessId

    //利用前端过来的数据进行计算得出最终的处置参考价
        @PutMapping("updateS")
        @ApiOperation("同意的评估报告")
        public Result updateS(@RequestBody Assess assess ){
            try {
                boolean flag = iAssessService.updateS(assess);
                if(flag){
                    return new Result(true , MessageConstant.EDIT_ASSESS_SUCCESS);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return new Result(false , MessageConstant.EDIT_ASSESS_FAIL);
        }

        //点击否决后，修改状态为评估为通过
        @PutMapping("updateDS")
        @ApiOperation("否决的评估报告")
        public Result updateDS(@RequestBody Assess assess)
        {
            try {
                boolean flag = iAssessService.updateDS(assess);
                if(flag){
                    return new Result(true , MessageConstant.EDIT_ASSESS_SUCCESS);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return new Result(false , MessageConstant.EDIT_ASSESS_FAIL);
        }




    @DeleteMapping("{id}")
    @ApiOperation("根据id删除评估报告")
    public Result delete (@PathVariable("id") Long id)
    {
        try {
            boolean flag=iAssessService.delete(id);
            if(flag)
            {
                return new Result(true,MessageConstant.DELETE_ASSESS_SUCCESS);
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.DELETE_ASSESS_FAIL);
    }
    @GetMapping("findAll")
    @ApiOperation("查询所有评估报告")
    public Result findAll()
    {
        try {
            List<Assess> list = iAssessService.list();
            if(list!=null)
            {
                return new Result(true,MessageConstant.QUERY_ASSESS_SUCCESS,list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_ASSESS_FAIL);
    }


}

